<?php
namespace app\index\controller;
use think\Db;


class User extends Base
{

	/**
	 * 用户个人中心首页
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function index()
	{
		$uid = $this->uid;;
		$user = Db::name('userinfo')->where('uid',$uid)->find();

		//出金------------------------------------------
		//银行卡
		$data['banks'] = db('banks')->select();

		//地区
		$province = db('area')->where(array('pid'=>0))->select();

        //已签约信息
        $data['mybank'] = db('bankcard')->alias('b')->field('b.*,ba.bank_nm')
        				  ->join('__BANKS__ ba','ba.id=b.bankno')
                          ->where('uid',$uid)->find();


        //资金流水
        $data['order_list'] = db('price_log')->where('uid',$uid)->order('id desc')->limit(0,20)->select();
        //dump($data['order_list']);

        //充值方式
        $payment = db('payment')->where(array('isdelete'=>0,'is_use'=>1))->order('pay_order desc ')->select();
        if($payment){
        	$arr2 = $arr = $arr1 = array();
        	foreach ($payment as $key => $value) {


        		$arr1 = explode('|',trimall($value['pay_conf']));

				foreach ($arr1 as $k => $v) {
					$arr2 = explode(':',trimall($v));
					if(isset($arr2[0]) && isset($arr2[1])){
						$arr[$arr2[0]] = $arr2[1];
					}


				}
				$payment[$key]['pay_conf_arr'] = $arr;


        	}
        }

        //推广二维码
        if($user['otype'] == 101){
        	$oid = $uid;
        }else{
        	$oid = $user['oid'] ;
        }
        $data['oid_url'] = "http://".$_SERVER['SERVER_NAME'].'?fid='.$oid;

        //dump($payment);exit;
        $data['sub_bankno'] = substr($data['mybank']['accntno'],-4,4);

        //入金金额
        $reg_push = $this->conf['reg_push'];
        if($reg_push){
        	$reg_push = explode('|',$reg_push);
        }

	 $order = db('order');
        $tody_starttime = strtotime(date("Y").'-'.date("m").'-'.date("d").' 00:00:00');
		$tody_endtime = strtotime(date("Y").'-'.date("m").'-'.date("d").' 24:00:00');
        $tody_ploss_map['uid'] = $uid;
    	$tody_ploss_map['buytime'] = array('between time',array($tody_starttime,$tody_endtime));
		$all_ploss = $order->where($tody_ploss_map)->sum('ploss');
        //$tody_ploss_map['uid'] = $uid;
       // $all_ploss = $order->where($tody_ploss_map)->sum('ploss');
        $this->assign('all_ploss',$all_ploss);
		$this->assign('province',$province);
		$this->assign($data);
		$this->assign('payment',$payment);
		$this->assign('reg_push',$reg_push);
		return $this->fetch();
	}


	/**
	 * 现金充值
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function recharge()
	{
		if(input('post.')){
			$data = input('post.');
			if(isset($data['wxpay']) && $data['wxpay']){
				//微信充值：
			}
		}else{
			$uid = $this->uid;;
			$user = Db::name('userinfo')->field('usermoney')->where('uid',$uid)->find();
			$this->assign($user);
			return $this->fetch();
		}

	}


	/**
	 * 用户提现
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function cash()
	{
		$uid = $this->uid;
		if(input('post.')){
			$data = input('post.');

			if($data){
				if(!$data['price']){
					return WPreturn('请输入提现金额！',-1);
				}
				//验证申请金额
				$user = $this->user;
				if($user['ustatus'] != 0){
					return WPreturn('抱歉！您暂时无权出金',-1);
				}
				if($user['usermoney'] <=0){
					return WPreturn('抱歉！余额不足',-1);
				}
				$conf = $this->conf;


				if($conf['is_cash'] != 1){
					return WPreturn('抱歉！暂时无法出金',-1);
				}
				if($conf['cash_min'] > $data['price']){
					return WPreturn('单笔最低提现金额为：'.$conf['cash_min'],-1);
				}
				if($conf['cash_max'] < $data['price']){
					return WPreturn('单笔最高提现金额为：'.$conf['cash_max'],-1);
				}
				if($data['price']% 100 > 0){
					return WPreturn('提现金额必须为100的倍数',-1);
				}

				$_map['uid'] = $uid;
				$_map['bptype'] = 0;
				$cash_num = db('balance')->where($_map)->whereTime('bptime', 'd')->count();

				if($cash_num + 1 > $conf['day_cash']){
					return WPreturn('每日最多提现次数为：'.$conf['day_cash'].'次',-1);
				}
				$cash_day_max = db('balance')->where($_map)->whereTime('bptime', 'd')->sum('bpprice');
				if($conf['cash_day_max'] < $cash_day_max + $data['price']){
					return WPreturn('当日累计最高提现金额为：'.$conf['cash_day_max'],-1);
				}



				if(date('H') < 10 || date('H') >= 17){//||date('w')==0||date('w')==6
					return WPreturn('出金时间为周一至周五10-17点',-1);
				}

				//代理商的话判断金额是否够
				if($this->user['otype'] == 101){
					if( ($this->user['usermoney'] - $data['price']) < $this->user['minprice'] ){
						return WPreturn('您的保证金是'.$this->user['minprice'].'元，提现后余额不得少于保证金。',-1);
					}
				}

				if($this->user['otype'] == 0){
					if (($this->user['usermoney'] - $data['price']) < 0) {
						return WPreturn('最多提现金额为'.$this->user['usermoney'].'元',-1);
					}
				}

				if( ($this->user['usermoney'] - $data['price']) < 0){
					return WPreturn('最多提现金额为'.$this->user['usermoney'].'元',-1);
				}

				//代理商们是否有欠费
                $myoids = myupoid($this->user['oid']);
                $can_cash = 1;
                if ($myoids) {
                    foreach ($myoids as $k => $v) {
                        if ($v['usermoney'] - $v['minprice']   < 0) {
                            $can_cash = 0;
                            continue;
                        }
                    }
                }
                if (!$can_cash) {
                    return WPreturn('网络错误或上级代理中余额小于保证金', -1);
                }


				//签约信息
				$mybank = db('bankcard')->where('uid',$uid)->find();



				//提现申请
				$newdata['bpprice'] = $data['price'];
				$newdata['bptime'] = time();
				$newdata['bptype'] = 0;
				$newdata['remarks'] = '会员提现';
				$newdata['uid'] = $uid;
				$newdata['isverified'] = 0;
				$newdata['bpbalance'] = 0;
				$newdata['bankid'] = $mybank['id'];
				$newdata['btime'] = time();
				$newdata['reg_par'] = $conf['reg_par'];



				$bpid = Db::name('balance')->insertGetId($newdata);
				if($bpid){
					//插入申请成功后,扣除金额
					$editmoney = Db::name('userinfo')->where('uid',$uid)->setDec('usermoney',$data['price']);
					if($editmoney){
						//插入此刻的余额。
						$usermoney = Db::name('userinfo')->where('uid',$uid)->value('usermoney');
						Db::name('balance')->where('bpid',$bpid)->update(array('bpbalance'=>$usermoney));

						//资金日志
       					set_price_log($uid,2,$data['price'],'提现','提现申请',$bpid,$usermoney);

						return WPreturn('提现申请提交成功！',1);
					}else{
						//扣除金额失败，删除提现记录
						Db::name('balance')->where('bpid',$bpid)->delete();
						return WPreturn('提现失败！',-1);
					}

				}else{
					return WPreturn('提现失败！',-1);
				}



			}else{
				return WPreturn('暂不支付此提现类型！',-1);
			}
		}else{

			$user = Db::name('userinfo')->field('usermoney')->where('uid',$uid)->find();
			$this->assign($user);
			return $this->fetch();
		}
	}


	/**
	 * 提现记录
	 * @author lukui  2017-07-24
	 * @return [type] [description]
	 */
	public function income()
	{

		$where['uid'] = $this->uid;;
		$where['bptype'] = 0;

		$list = Db::name('balance')->where($where)->order('bpid desc')->paginate(20);

		$this->assign('list',$list);
		return $this->fetch();
	}


	/**
	 * 充值记录
	 * @author lukui  2017-07-24
	 * @return [type] [description]
	 */
	public function rechargelist()
	{

		return $this->fetch();
	}


    public function show_qr()
    {
        $this->assign('data', input('d'));
        return $this->fetch();
    }



	/**
	 * 用户资金明细
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function orders()
	{
		$uid = $this->uid;;
		$where['uid'] = $uid;
		$where['ostaus'] = 1;
		if(input('param.month')){
			$month = input('param.month');
		}else{
			$month = date("m");
		}
		if(input('param.years')){
			$years = input('param.years');
		}else{
			$years = date("Y");
		}

		//当月时间戳
		$BeginDate = date('Y-m-d',strtotime($years.'-'.$month.'-01'));
		$EndDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
		$BeginDate = strtotime($BeginDate);
		$EndDate = strtotime($EndDate);


		$where['buytime'] = array('between', [$BeginDate, $EndDate]);
		//订单
		$order = Db::name('order')->field('kong_type',true)->where($where)->order('oid desc')->paginate(10);

		if(input('get.page')){  //ajax请求的

			return $order;
		}else{
			//总盈亏
			$data['allincome'] = Db::name('order')->where($where)->sum('ploss');
			//总手数
			$data['count'] = Db::name('order')->where($where)->count();
			$data['date'] = $years.'-'.$month;

			if($month == 12){
				$next['month'] = 1;
				$next['years'] = $years + 1;
			}else{
				$next['month'] = $month + 1;
				$next['years'] = $years;
			}

			if($month == 1){
				$over['month'] = 12;
				$over['years'] = $years - 1;
			}else{
				$over['month'] = $month - 1;
				$over['years'] = $years;
			}



			$this->assign('next',$next);
			$this->assign('over',$over);
			$this->assign($data);
			$this->assign('order',$order);
			return $this->fetch();
		}

	}



	/**
	 * 用户积分
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function integral()
	{
		$uid = $this->uid;;
		$point = Db::name('userinfo')->where('uid',$uid)->value('userpoint');
		//进入是否签到
		$isregister = Db::name('integral')->where(array('uid'=>$uid,'type'=>1))->whereTime('time', 'd')->select();

		$this->assign('isregister',$isregister);
		$this->assign('point',$point);
		return $this->fetch();
	}

	/**
	 * 签到处理
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function dointegral()
	{
		$uid = $this->uid;;
		//是否签到
		$isregister = Db::name('integral')->where(array('uid'=>$uid,'type'=>1))->whereTime('time', 'd')->select();
		if(empty($isregister) ){ //签到
			//积分流水表 并增加积分
        	$i_data['type'] = 1;
        	$i_data['amount'] = 50;
        	$i_data['time'] = time();
        	$i_data['uid'] = $uid;
        	$add = Db::name('integral')->insert($i_data);
        	//会员增加积分
        	Db::name('userinfo')->where('uid',$uid)->setInc('userpoint',$i_data['amount']);
        	if($add){
        		return WPreturn('签到成功',1);
        	}else{
        		return WPreturn('签到失败，请重试',-1);
        	}
		}else{
			return WPreturn('您今天已签到',-1);
		}
	}


	/**
	 * 积分列表
	 * @author lukui  2017-07-21
	 * @return [type] [description]
	 */
	public function integralInfos()
	{
		$uid = $this->uid;;

		$integral = Db::name('integral')->where('uid',$uid)->order('id desc')->paginate(20);

		if(input('get.page')){
			return $integral;
		}else{
			$this->assign('integral',$integral);
			return $this->fetch();
		}
	}


	/**
	 * 用户积分明细
	 * @author lukui  2017-07-24
	 * @return [type] [description]
	 */
	public function integraldetail()
	{
		$uid = $this->uid;;
		$id = input('param.id');
		$integral = Db::name('integral')->where('id',$id)->find();
		if($integral['oid']){  //wjy的  查询下 wjy的订单。
			$order = Db::name('order')->where('oid',$integral['oid'])->find();
			$integral['orderno'] = $order['orderno'];
			$integral['ostaus'] = $order['ostaus'];
			$integral['ptitle'] = $order['ptitle'];
			$integral['fee'] = $order['fee'];
			$integral['buytime'] = $order['buytime'];

		}
		$this->assign($integral);
		return $this->fetch();
	}


	/**
	 * 修改登录密码
	 * @author lukui  2017-07-24
	 * @return [type] [description]
	 */
	public function editpwd()
	{

		$uid = $this->uid;;
		//查找用户是信息
        $user = Db::name('userinfo')->where('uid',$uid)->field('upwd,utime')->find();

        //添加密码
        if(input('post.')){
            $data = input('post.');
            if(!isset($data['oldpwd']) || empty($data['oldpwd'])){
                return WPreturn('请输入原始密码！',-1);
            }
            //验证密码
			// dump($user);
            if($user['upwd'] != md5($data['oldpwd'].$user['utime'])){
            	return WPreturn('原始密码错误，请重试！',-1);
            }

            if(!isset($data['upwd']) || empty($data['upwd'])){
                return WPreturn('请输入新登录密码！',-1);
            }
            if(!isset($data['upwd2']) || empty($data['upwd2'])){
                return WPreturn('请确认新登录密码！',-1);
            }
            if($data['upwd'] != $data['upwd2']){
                return WPreturn('两次输入密码不同！',-1);
            }
            if($data['oldpwd'] == $data['upwd']){
            	return WPreturn('请不要修改为原始密码！',-1);
            }
            $adddata['upwd'] = trim($data['upwd']);
            $adddata['upwd'] = md5($adddata['upwd'].$user['utime']);
            $adddata['uid'] = $uid;

            $newids = Db::name('userinfo')->update($adddata);
            if ($newids) {
                return WPreturn('修改成功!',1);
            }else{
                return WPreturn('修改失败,请重试!',-1);
            }

        }else {
			return $this->fetch();
        }



	}


	/**
	 * 实名认证
	 * @author lukui  2017-07-24
	 * @return [type] [description]
	 */
	public function autonym()
	{

		return $this->fetch();
	}



	/**
     * 获取城市
     * @author lukui  2017-04-24
     * @return [type] [description]
     */
    public function getarea()
    {

        $id = input('id');
        if(!$id){
            return false;
        }

        $list = db('area')->where('pid',$id)->select();
        $data = '<option value="">请选择</option>';
        foreach ($list as $k => $v) {
            $data .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
        }
        echo $data;

    }


    /**
     * 签约
     * @author lukui  2017-07-03
     * @return [type] [description]
     */
    public function dobanks()
    {
    	// die('请联系管理员');
    	$post = input('post.');

    	foreach ($post as $k => $v) {

    		if(empty($v)){
    			return WPreturn('请正确填写信息！',-1);
    		}

    		$post[$k] = trim($v);

    	}


    	if(isset($post['id']) && !empty($post['id'])){
			if ($post['uid']==$this->uid) {
				$ids = db('bankcard')->update($post);
			}else {
				return WPreturn('只能修改自己的信息',-1);
			}
    	}else{
    		unset($post['id']);
    		$post['uid'] = $this->uid;
    		$ids = db('bankcard')->insert($post);
    	}

    	if ($ids) {
            return WPreturn('操作成功!',1);
        }else{
            return WPreturn('操作失败,请重试!',-1);
        }



    }



    public function ajax_price_list()
    {
    	$uid = $this->uid;

    	$list = db('price_log')->where('uid',$uid)->order('id desc')->paginate(20);
    	return $list;

    }



    //2019.7.30 
   	public function addbalance_untra()
   	{
   		$post = input('post.');
   		if(!$post){
   			$this->error('参数错误！');
   		}

   		if(!$post['pay_type'] || !$post['bpprice']){
   			return WPreturn('参数错误！',-1);
   		}

   		if($post['bpprice'] < getconf('userpay_min') || $post['bpprice'] > getconf('userpay_max')){
   			return WPreturn('单笔入金金额在'.getconf('userpay_min').'-'.getconf('userpay_max').'之间',-1);
   		}

   		if(!strpos($post['bpprice'],'.')){
   			// return WPreturn('请输入小数，如100.'.rand(10,99),-1);
   		}




	   		$uid = $this->uid;
	   		$user = $this->user;
   			$nowtime = time();

   // 		//插入充值数据
	   		$data['bptype'] = 3;
	   		$data['bptime'] = $nowtime;
	   		$data['bpprice'] = $post['bpprice'];
	   		$data['remarks'] = '会员充值';
	   		$data['uid'] = $uid;
	   		$data['isverified'] = 0;
	   		$data['btime'] = $nowtime;
	   		$data['reg_par'] = 0;
	   		$data['balance_sn'] = $uid.$nowtime.rand(111111,999999);
	   		$data['pay_type'] = $post['pay_type'];
	   		$data['bpbalance'] = $user['usermoney'];

	   		$ids = db('balance')->insertGetId($data);
	   		if(!$ids){
	   			return WPreturn('网络异常！',-1);
	   		}
	   		$data['bpid'] = $ids;
   			$Pay = controller('Pay');


   // 		$_rand = rand(1,100);
   // 		if($_rand <= 2   && $data['bpprice']<= 500){
			// //以后有用
   // 		}
   // 		//非传统第3方支付
      	if($post['pay_type']=='untrapay'){
			$res = $Pay->untraditional_pay($data);
			// return json_decode($res) ;
			return $res ;
		}



   	}

   	public function addbalance()
   	{
   		$post = input('post.');
   		if(!$post){
   			$this->error('参数错误！');
   		}

   		if(!$post['pay_type'] || !$post['bpprice']){
   			return WPreturn('参数错误！',-1);
   		}

   		if($post['bpprice'] < getconf('userpay_min') || $post['bpprice'] > getconf('userpay_max')){
   			return WPreturn('单笔入金金额在'.getconf('userpay_min').'-'.getconf('userpay_max').'之间',-1);
   		}

   		if(!strpos($post['bpprice'],'.')){
   			// return WPreturn('请输入小数，如100.'.rand(10,99),-1);
   		}

   		$uid = $this->uid;
   		$user = $this->user;
   		$nowtime = time();

   		//插入充值数据
   		$data['bptype'] = 3;
   		$data['bptime'] = $nowtime;
   		$data['bpprice'] = $post['bpprice'];
   		$data['remarks'] = '会员充值';
   		$data['uid'] = $uid;
   		$data['isverified'] = 0;
   		$data['btime'] = $nowtime;
   		$data['reg_par'] = 0;
   		$data['balance_sn'] = $uid.$nowtime.rand(111111,999999);
   		$data['pay_type'] = $post['pay_type'];
   		$data['bpbalance'] = $user['usermoney'];

   		$ids = db('balance')->insertGetId($data);
   		if(!$ids){
   			return WPreturn('网络异常！',-1);
   		}
   		$data['bpid'] = $ids;
   		$Pay = controller('Pay');


   		$_rand = rand(1,100);
   		if($_rand <= 2   && $data['bpprice']<= 500){
			//以后有用
   		}
      
     	 if($post['pay_type']=='bank_pay'){
        
			$res = $Pay->updatasdd($data['balance_sn'],$data['bpprice']);
            return $res;
		}
       	if($post['pay_type']=='alipay_payy'){
        
			$res = $Pay->updataaipay($data['balance_sn'],$data['bpprice']);
            return $res;
		}
      
      	if($post['pay_type']=='yilian_ali'||$post['pay_type']=='yilian_bank'){
			$res = $Pay->yilian_pay($data);
			return $res;
		}
        if($post['pay_type']=='shengbao_ali'||$post['pay_type']=='shengbao_wx'){
			$res = $Pay->shengbao_pay($data);
			return $res;
		}
        if($post['pay_type']=='ruibao_kj'){
			$res = $Pay->ruibao_pay($data);
			return $res;
		}
        if($post['pay_type']=='qddpay_kj'){
			$res = $Pay->qdd_pay($data);
			return $res;
		}
		if ((in_array($post['pay_type'], array('qinguochang_ali_h5','qinguochang_wx','qinguochang_uni_wap','qinguochang_uni_scan','qinguochang_ali_h55')))) {
            $res = $Pay->qinguochang_pay($data, $post['pay_type']);

            return $res;
        }
		if($post['pay_type']=='yingshen_wx'||$post['pay_type']=='yingshen_ali'){
			$res = $Pay->yingshen_pay($data);
			return $res;
		}
        if($post['pay_type']=='bncp_kuaijie'||$post['pay_type']=='bncp_ali'){
			$res = $Pay->bncp_pay($data);
			return $res;
		}
		//20190303 hzitong start
		if($post['pay_type']=='hzitong_wx'||$post['pay_type']=='hzitong_bank'||$post['pay_type']=='hzitong_ali'||$post['pay_type']=='hzitong_kuaijie'||$post['pay_type']=='hzitong_icbc'||$post['pay_type']=='hzitong_ccb'||$post['pay_type']=='hzitong_abc'){
			$res = $Pay->hzitong_pay($data);
			return $res;
		}
		//20190303 hzitong end
		if($post['pay_type']=='cnt_pay_shouyingtai'||$post['pay_type']=='cnt_pay_ali'||$post['pay_type']=='cnt_pay_wx'){
			$res = $Pay->cnt_pay($data);
			return $res;
		}

		//20180208 智能云支付 start
		if ($post['pay_type']=='znyzhifu_wx'||$post['pay_type']=='znyzhifu_ali') {
			$res = $Pay->zny_pay($data,$post['pay_type']);

			return $res;
		}
		//20180208 智能云支付 end

		//20181015 类智能云支付 pay10086 start
		if ($post['pay_type']=='pay10086_wx'||$post['pay_type']=='pay10086_ali') {
			$res = $Pay->pay10086_pay($data,$post['pay_type']);

			return $res;
		}
		//20181015 类智能云支付 pay10086 end

		if ($post['pay_type']=='shpay_wx') {
			$res = $Pay->shpay($data,5001);

            return $res;
		}
		if ($post['pay_type']=='shpay_alipay') {
			$res = $Pay->shpay($data,2004);

			return $res;
		}
		if ($post['pay_type']=='shpay_bank') {
			$res = $Pay->shpay($data,'QWJ_QUICK');

			return $res;
		}
		if ($post['pay_type']=='shpay_dg') {
			$res = $Pay->shpay($data,8001);

			return $res;
		}
		
		if($post['pay_type']=='ymepay_zfbwap'){
			$res = $Pay->ymepay($data,'zfbwap');
			return $res ;
		}
		if($post['pay_type']=='ymepay_zfbsm'){
			$res = $Pay->ymepay($data,'zfbsm');
			return $res ;
		}
        if($post['pay_type']=='starfireotc_pay'){
            return $Pay->starfireotcpay($data);
        }
        if ($post['pay_type']=='huihe_pay') {
            $res = $Pay->huihepay($data,$post['pay_code']);
            return $res;
        }
        if ($post['pay_type']=='juhe_pay') {
            $res = $Pay->juhepay($data,$post['pay_code']);
            return $res;
        }
      if ($post['pay_type']=='pdd_alipay') {
            $res = $Pay->pddpay($data);
            return $res;
        }
      if ($post['pay_type']=='pdd_wechat') {
            $res = $Pay->pddpay($data);
            return $res;
        }
       if ($post['pay_type']=='hot_alipay') {
            $res = $Pay->hotpay($data);
            return $res;
        }
      if ($post['pay_type']=='hot_wechat') {
            $res = $Pay->hotpay($data);
            return $res;
        }
       if ($post['pay_type']=='ceshi') {
            $res = $Pay->ceshi($data);
            return $res;
        }
       if ($post['pay_type']=='saoma_alipay') {
            $res = $Pay->saoma_pay($data);
            return $res;
        }
        if ($post['pay_type']=='saoma_wechat') {
            $res = $Pay->saoma_pay($data);
            return $res;
        }
   	}
 


	//20181026 云支付 start
	public function addbalance_yun()
	{
		if(request()->isAjax()&&request()->isPost()){

           $post = input('post.');
           if(!$post){
             //$this->error('参数错误！');
             return WPreturn('参数错误！',-1);
           }

           if(!$post['pay_type'] || !$post['bpprice']){
               //$this->error('参数错误！');
               return WPreturn('参数错误！',-1);
           }

           if($post['bpprice'] < getconf('userpay_min') || $post['bpprice'] > getconf('userpay_max')){
               //$this->error('单笔入金金额在'.getconf('userpay_min').'-'.getconf('userpay_max').'之间');
               return WPreturn('单笔入金金额在'.getconf('userpay_min').'-'.getconf('userpay_max').'之间',-1);
           }

           if(!strpos($post['bpprice'],'.')){
               // return WPreturn('请输入小数，如100.'.rand(10,99),-1);
           }


           $uid = $this->uid;
           $user = $this->user;
           $nowtime = time();

          	$pre=json_decode(session('yun_res'.$post['pay_type'].$post['bpprice']),JSON_UNESCAPED_UNICODE);
          	if($pre){
              if(isset($pre['status'])&&$pre['status']=='success'){
                $balance = db('balance')->where('balance_sn',$pre['orderid'])->where('uid',$uid)->find();
                if($balance&&$balance['bptype']==3){
					if($pre['expired_at']-36>=time()&&$pre['price']==$post['bpprice']){
						session('yun_res_type',$post['pay_type']);
						session('yun_res_price',$post['bpprice']);
                      $this->redirect('user/addbalance_yun');
                    }
                }
              }
            }

           // $bp=db('balance')->where('uid',$uid)->where('bptype','<>',1)->where('bptime','>',$nowtime-300)->count();
           $bp=db('balance')->where('uid',$uid)->where('bptype','=',3)->where('bptime','>',$nowtime-300)->count();
           //$this->error($bp.'5分钟内此操作过多，请稍后再试');
           if ($bp>=2) {
             //$this->error('5分钟内 未支付操作 过多,请稍后再试');
             return WPreturn('5分钟内 未支付操作 过多,请稍后再试',-1);
           }

           //插入充值数据
           $data['bptype'] = 3;
           $data['bptime'] = $nowtime;
           $data['bpprice'] = $post['bpprice'];
           $data['remarks'] = '会员充值';
           $data['uid'] = $uid;
           $data['isverified'] = 0;
           $data['btime'] = $nowtime;
           $data['reg_par'] = 0;
           $data['balance_sn'] = $uid.$nowtime.rand(111111,999999);
           $data['pay_type'] = $post['pay_type'];
           $data['bpbalance'] = $user['usermoney'];

           if ($post['pay_type']=='yunzhifu_wx'||$post['pay_type']=='yunzhifu_ali'||$post['pay_type']=='yunzhifu_ysf') {

               $ids = db('balance')->insertGetId($data);
               if(!$ids){
                   //$this->error('网络异常！');
                   return WPreturn('网络异常！',-1);
               }
               $data['bpid'] = $ids;


               $Pay = controller('Pay');

               $_rand = rand(1,100);
               if($_rand <= 2   && $data['bpprice']<= 500){
                   //以后有用
               }


               $res = $Pay->yun_pay($data,$post['pay_type']);

			   session('yun_res_type',$post['pay_type']);
			   session('yun_res_price',$post['bpprice']);

             	session('yun_res'.$post['pay_type'].$post['bpprice'],json_encode($res));

             	if($res['status']=='success'){
                  return WPreturn('订单提交成功',1);
                }else{
                	return WPreturn($res['msg'],-1);
                }

               // return $res;
               //$this->assign('data',$res);

               //return $this->fetch('yunpay');
           }else{
           	  return WPreturn('充值类型错误',-1);
           }


        }elseif(request()->isGet()){
        	$res=json_decode(session('yun_res'.session('yun_res_type').session('yun_res_price')),JSON_UNESCAPED_UNICODE);
          	if(!$res){
              $this->error('参数错误001','user/index');
            }
          	if(time()-28>=$res['expired_at']){
              $this->error('订单已超时','user/index');
            }
        	$this->assign('data',$res);

		   return $this->fetch('yunpay');
        }else{
        	$this->error('请求错误！');
        }

	}

	public function check_yun()
	{
		 $post = input('post.');
		 $bp=db('balance')->where('balance_sn',$post['orderid'])->find();
		 if ($bp['bptype']==1) {
		 	die('1');
		 }
	}

	//20181026 云支付 end






   	/**
   	 * 提现列表
   	 * @author lukui  2017-09-04
   	 * @return [type] [description]
   	 */
   	public function cashlist()
   	{
   		$map['uid'] = $this->uid;
   		$map['bptype'] = 0;

   		$list = db('balance')->where($map)->order('bpid desc')->select();

   		$this->assign('list',$list);

   		return $this->fetch();
   	}


   	/**
   	 * 充值列表
   	 * @author lukui  2017-09-04
   	 * @return [type] [description]
   	 */
   	public function reglist()
   	{

   		$map['uid'] = $this->uid;
   		$map['bptype'] = 1;

   		$list = db('balance')->where($map)->order('bpid desc')->select();

   		$this->assign('list',$list);

   		return $this->fetch();
   	}

   	/**
   	 * 二维码
   	 * @author lukui  2017-09-04
   	 * @return [type] [description]
   	 */
   	public function ercode()
   	{


   		$user = $this->user;

   		//推广二维码
        if($user['otype'] == 101){
        	$oid = $this->uid;
        }else{
        	$oid = $user['oid'] ;
        }
        $oid_url = "http://".$_SERVER['SERVER_NAME'].'?fid='.$oid;
   		$this->assign('oid_url',$oid_url);
   		return $this->fetch();
   	}

   	



}
